package com.gzx.spring.apis.service;

import org.apache.commons.io.IOUtils;
import org.apache.http.conn.HttpHostConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzx.risk.core.apis.result.RiskResult;

public class ApiExec {
	private static Logger LOGGER = LoggerFactory.getLogger(ApiExec.class);

	public static RiskResult exec(Object obj, String method, MyConsumer<Object> task) {
		try {
			return task.accept();
		} catch (HttpHostConnectException e) {
			if (e.getCause() != null && e.getCause().getMessage().indexOf("timed out") >= 0) {
				LOGGER.info("apiName:{},timeout", obj.getClass().getSimpleName() + "." + method);
			} else {
				LOGGER.error("apiName:{} error", obj.getClass().getSimpleName() + "." + method, e);
			}
		} catch (Exception e) {
			LOGGER.error("apiName:{} error", obj.getClass().getSimpleName() + "." + method, e);
		}
		return null;
	}

	public static String loadJson(String name) throws Exception {
		return IOUtils.toString(ApiExec.class.getResourceAsStream(name + ".json"), "UTF-8");
	}
}
